/*
 * $RCSfile: Processor_46_Msg.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-16  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.dzpk.gameserver.module.main.processor;

import java.util.Map;

import org.apache.log4j.Logger;

import com.i366.Constant;
import com.i366.cache.Cache;
import com.i366.room.TsUtil;
import com.i366.room.model.RoomPersion;
import com.i366.room.model.impl.Room;
import com.i366.service.RoomService;
import com.work.client.util.I366ClientPickUtil;
import com.work.comm.util.I366PickUtil;
import com.work.comm.util.PublisherUtil;
import com.work.db.UserInfoDao;
import com.work.db.imp.UserInfoDaoImp;
import com.work.db.model.UserInfo;
import com.work.engine.protocal.request.Request;
import com.work.engine.server.handler.Handler;

/**
 * 
 * ʹ�õ���
 * <p>Title: Processor_62_Persion</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class Processor_63_Gif extends Handler{

	private Logger logger = Logger.getLogger(Processor_63_Gif.class);
	public byte[] handleRequest() {
		logger.debug("Processor_63_Gif userId is : " + this.getGameRequest().getUserId());
		Request request = getGameRequest();
		RoomService.updateUser(request);
		int[][] int2 = {{60,I366ClientPickUtil.TYPE_INT_1},
				{61,I366ClientPickUtil.TYPE_INT_1},
				{62,I366ClientPickUtil.TYPE_INT_1},
				{130,I366ClientPickUtil.TYPE_INT_4}
		};
		
		Map<Integer, Object> map = I366PickUtil.pickAll(request.getBt(), int2);
		int fromSeat = (Integer)map.get(60);
		int toSeat = (Integer)map.get(61);
		int giftID = (Integer)map.get(62);
		int chips = (Integer)map.get(130);
		int userId = request.getUserId();
		int inUserId = 0;
		RoomPersion inRoomPersion = null;
		try {
			UserInfo userInfo = Cache.getOnlineUserInfo().get(userId);
			if (userInfo == null) {
				return pusUser(1); 
			}
			Room room = Cache.getRoom(userInfo.getRoomId(), userInfo.getRoomPath());
			RoomPersion rp = room.getAudMap().get(userId);
			inRoomPersion = room.getRoomPersions()[toSeat];
			inUserId = inRoomPersion.getUserId();
			//��֤ʹ������
			UserInfoDao dao = new UserInfoDaoImp();
			if (giftID != 0 && userInfo.getUserProductByList() !=  null && userInfo.getUserProductByList().getGifCount() > 0) {
				chips = 0;
				room.getRoomService().liwu(chips, rp, fromSeat, toSeat, giftID, inRoomPersion , userInfo.getUserProductByList());
			}else {
				if (giftID != 0 || rp == null || rp.getChouma() < chips || chips < 1) {
					return pusUser(1);
				}
				if (giftID == 0) {
					if (System.currentTimeMillis() - rp.getLiwuTime1() < Constant.LIWU_TIME1) {
						return pusUser(1);
					}
					int allChip = dao.getUserLiwuChipsTo1(userId);
					if (allChip >= Constant.ALL_LIWU_1_CHIPS) {
						return pusUser(1);
					}
				}else if (System.currentTimeMillis() - rp.getLiwuTime2() < Constant.LIWU_TIME2) {
					return pusUser(1);
				}
				//--------
				room.getRoomService().liwu(chips, rp, fromSeat, toSeat, giftID, inRoomPersion , null);
			}

			//��¼LOG
			dao.inserUserLiwuChips(userId, chips, String.valueOf(inUserId), room.getRoomId(), giftID);
			//
			try {
				Thread.sleep(2000);
			} catch (Exception e) {
			}
			String str = null;
			if (chips == 0) {
				str = "�������һ�����ߣ�";
			}else {
				str = "�����" + chips + "���룡";
			}
			byte[] bytes = TsUtil.getSendByte(str);
			if (giftID == 0) {
				str = "������" + userInfo.getNikeName()  + "���͵�" + chips + "����!";
				byte[] bytes1 = TsUtil.getSendByte(str);
				PublisherUtil.publisher(inRoomPersion.getUserInfo(), bytes1);
			}else {
				str = userInfo.getNikeName() +  "����ʹ���˵��ߣ�";
				byte[] bytes2 = TsUtil.getSendByte(str);
				PublisherUtil.publisher(inRoomPersion.getUserInfo(), bytes2);
			}
			return bytes;
		} catch (Exception e) {
			logger.debug("������Ϣ����", e);
			return pusUser(1);
		}
	}

	public byte[] pusUser(int status) {
		Object[][] objs2 = {
				{60,status,I366ClientPickUtil.TYPE_INT_1}
		};
		byte[] bytes = I366PickUtil.packAll(objs2, Constant.REQ_GAME_USE_GIFT);
		return bytes;
	}
	
	public Object makeObject() throws Exception {
		return new Processor_63_Gif();
	}
	
}

